import { _decorator, Component, Node, Label, director, sys, Material } from 'cc';
import { TurkishTetris_GameData } from "../../../../hall/Data/TurkishTetris_GameData";
import { TurkishTetris_GameStorage } from "../../../../hall/Data/TurkishTetris_GameStorage";
import TurkishTetris_GameFunctions from "../Common/TurkishTetris_GameFunctions";
import TurkishTetris_GameManager from "../Manager/TurkishTetris_GameManager";
import TurkishTetris_UIManager from "../Manager/TurkishTetris_UIManager";
import { SSS_AudioManager } from 'db://assets/Script/Manager/SSS_AudioManager';

const { ccclass, property } = _decorator;

@ccclass('TurkishTetrisGamePanel')
export default class TurkishTetris_GamePanel extends Component {
    static Instance: TurkishTetris_GamePanel;
    @property(Node)
    Button_back: Node | null = null;
    @property(Node)
    Button_refresh: Node | null = null;
    @property(Label)
    Label_score: Label | null = null;
    @property(Label)
    Label_highScore: Label | null = null;

    chainNumTxt: Label | null = null;

    onLoad() {
        TurkishTetris_GamePanel.Instance = this;
    }

    ShowOn() {
        this.node.active = true;
        this.AddListener();
        this.Refresh(true);
    }
    ShowOff() {
        this.node.active = false;

        this.RemoveListener();
    }
    Refresh(isInit: boolean = false) {
        if (TurkishTetris_GameData.gameMode == 0) {
            this.Label_score.string = `${TurkishTetris_GameData.GetScore()}`;
            this.Label_highScore.string = `${TurkishTetris_GameStorage.data.high_score}`;

            // this.Label_highScore.string = `最高纪录：${TurkishTetris_GameData.curScore > TurkishTetris_GameData.high_score ? TurkishTetris_GameData.curScore : TurkishTetris_GameData.high_score}`;

            let isWeiXian = TurkishTetris_GameFunctions.IsWeiXian();
            // console.log(`isWeiXian---------`, isWeiXian, GameData.arrCardGroups[4].length);
            TurkishTetris_UIManager.Instance.DeadFrame.active = isWeiXian;
        }
    }
    AddListener() {
        this.Button_back.on(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.on(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }
    RemoveListener() {
        this.Button_back.off(Node.EventType.TOUCH_END, this.On_Button_back, this);
        this.Button_refresh.off(Node.EventType.TOUCH_END, this.On_Button_refresh, this);
    }
    On_Button_back() {
        director.loadScene("MainScene");

        SSS_AudioManager.PlayButtonAudio();
    }
    On_Button_refresh() {
        TurkishTetris_GameManager.Instance.ReStart();

        // let currentSceneName = director.getScene().name;
        // director.loadScene(currentSceneName);

        SSS_AudioManager.PlayButtonAudio();
    }
}

// /** 调试 */
window["TurkishTetris_GamePanel"] = TurkishTetris_GamePanel;